var config = require('../../../../config');
const adrcUrl = config.adrcUrl;
const proaUrl = config.proaUrl;
const payodrUrl = config.payodrUrl;
const ntyUrl = config.ntyUrl;
const odrUrl = config.odrUrl;


var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    items: [
      { name: 'CHN', value: '货到付款（现金）', checked: 'true' },
      { name: 'USA', value: '货到付款（刷卡）' },
    ],
    showView: false,
    'num':1,
    'price':200,
    'money':200,
    'pinkage':38,
    'hesuan':202,
    'shipp':10,
  },
/**
 * 显示与隐藏
 */
  onChangeShowState: function () {
    var that = this;
    that.setData({
      showView: (!that.data.showView)
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    showView: (options.showView == "true" ? true : false);
    var that=this;
   
    /*通讯地址*/
   
    wx.chooseAddress({
      success: function (res) {
        var adr = res;
        app.getUserOpenId(function (err, openid) {
          if (!err) {
            wx.request({
              url: adrcUrl, //仅为示例，并非真实的接口地址
              data: {
                userid: openid,
                username: res.userName,
                telnumber: res.telNumber,
                provincename: res.provinceName,
                cityname: res.cityName,
                countyname: res.countyName,
                detailinfo: res.detailInfo,
              },
              method: 'post',
              header: {
                'content-type': 'application/json'
              },
              success: function (res) {
                console.log(res);
                that.setData({
                  adr: adr,
                  adrid:res.data.id
                })
              }
            });
          }
        })        
      },
      fail:function(e){
        wx.showModal({
          title: '收货地址提示',
          content: '您没有填写收货地址,去填写收货地址',
          success: function (res) {
            if (res.confirm) {
              wx.navigateTo({
                url: '/pages/myinfo/site/site_add/site_add'
              })
            } else if (res.cancel) {

            }
          }
        })
      }
    });

    /*end 通讯地址*/


    /*设置产品信息*/
    var value = wx.getStorageSync('details');
    if (value) {
      
      var num = that.data.num;
      if (parseInt(value.num) > 1) {
        num = value.num
      }      
      var act=[];
      if (value.actvid){
          wx.request({
            url: proaUrl +value.actvid,
            success:function(e){
                act=e.data;
            }
          })
      }
      var sul = common.activity(act, num, value.money, 10);
      var hesuan = sul['mn'];
      //sul['pro'] = prostr;
      this.setData({
        details: value,
        price: value.price,
        money: value.money,
        pinkage: 38,
        shipp: 10,
        hesuan: hesuan,
        proid: value.id,
        odrid: value.odrid,
        num: num,
        act:act
      })
    }
    /*end设置产品信息*/


  },
  /**
   * 点击减号数组递减
   */
  onJianHao: function () {
    var num = this.data.num;//数量
    var pinkage = this.data.pinkage;//免费运费金额
    var shipp = this.data.shipp;//运费
    var price = this.data.money;//单价
    var act = this.data.act;//活动
    num = num - 1;
    var sul = common.activity(act, num, price, 10);
    var hesuan = sul['mn'];
    if (num > 0){
      this.setData({
        num:num,
        hesuan: hesuan
      })
    }
  },
  /**
   * 点击加号数组叠加
   */
onJiaHao:function(){
  var num = this.data.num;//数量
  var price = this.data.money;//单价
  var pinkage = this.data.pinkage;//免费运费金额
  var shipp = this.data.shipp;//运费
  var act = this.data.act;//活动
  num = num + 1;
  var sul = common.activity(act, num, price, 10);
  var hesuan = sul['mn'];

  this.setData({
    hesuan: hesuan,
    num: num,
  })                  
},
  /**
   * 微信支付
   */
  onWeChatPay:function(){

    var self = this

    self.setData({
      loading: true
    })
    // 此处需要先调用wx.login方法获取code，然后在服务端调用微信接口使用code换取下单用户的openId
    // 具体文档参考https://mp.weixin.qq.com/debug/wxadoc/dev/api/api-login.html?t=20161230#wxloginobject
   
    app.getUserOpenId(function (err, openid) {
      var paymentUrl = payodrUrl;
      console.log(openid);
      if (!err) {       
        wx.request({
          url: paymentUrl,  
          data: {
            id: self.data.odrid,
          },
          method:'POST',
          success: function (res) {
            var id=res.data.id;
            var payargs = res.data
            wx.requestPayment({
              timeStamp: payargs.timeStamp.toString(),
              nonceStr: payargs.nonce_str,
              package: 'prepay_id='+payargs.prepay_id,
              signType: 'MD5',
              paySign: payargs.paySign,
              success: function (res) {            
                wx.request({
                  url: ntyUrl,
                  data: {
                    id: id,                     
                  },
                  method: 'POST',
                  success: function (res) {

                  }
                })

                wx.redirectTo({
                  url: '/pages/myinfo/order/myorder/myorder'
                });
              }
              
            })
          }
        })
      } 
    })
  },
  //货到付款
  ordert:function(){
    var self = this;
    app.getUserOpenId(function (err, openid) {
      var paymentUrl = odrUrl + openid + '/' + self.data.proid + '/' + self.data.ardid + '/' + self.data.num;
    
      if (!err) {
        wx.request({
          url: paymentUrl,
          success: function (res) {
            wx.redirectTo({
              url: '/pages/myinfo/order/myorder/myorder'
            })
          }
        })
      }
    });
  },
  //更换收货地址
  adrup:function(){
    wx.chooseAddress({
      success: function (res) {
        var adr = res;
        app.getUserOpenId(function (err, openid) {
          if (!err) {
            wx.request({
              url: adrcUrl, //仅为示例，并非真实的接口地址  
              data: {
                userid: openid,
                username: res.userName,
                telnumber: res.telNumber,
                provincename: res.provinceName,
                cityname: res.cityName,
                countyname: res.countyName,
                detailinfo: res.detailInfo,
              },
              method: 'post',
              header: {
                'content-type': 'application/json'
              },
              success: function (res) {
                console.log(res);
                that.setData({
                  adr: adr,
                  adrid: res.data.id
                })
              }
            });
          }
        })
      },
    })
  }
 
})